package waal.pc.frontend;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import waal.pc.frontend.tiles.Tile;
import waal.pc.frontend.tiles.UpdateableTile;

public class TileTab {

	private Surface surface;
	private JPanel panel;
	private GridBagLayout gbl;
	private Map<Integer, TileTopTile> tiles;
	
	public TileTab(Surface surface) {
		this.surface = surface;
		this.gbl = new GridBagLayout();
		this.tiles = new HashMap<Integer, TileTopTile>();
		
		this.createPane(3,2);		
	}
	
	public JPanel getPane() {
		return this.panel;
	}
	
	private void createPane(int colum, int row) {
		this.panel = new JPanel(this.gbl);
		int c, r, i=0;
		TileTopTile tt;
		for(r=0; r < row; r++ ) {
			for(c=0; c < colum; c++) {
				i++;
				tt = new TileTopTile(i);
				this.tiles.put(i, tt);
				this.addComponent(tt, c, r, 1, 1, 1, 1);
			}
			
		}
	}
	
	private void addComponent(Component c, int x, int y, int width, int height, double weightx, double weighty ) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = x; gbc.gridy = y;
		gbc.gridwidth = width; gbc.gridheight = height;
		gbc.weightx = weightx; gbc.weighty = weighty;
		this.gbl.setConstraints( c, gbc );
		c.setPreferredSize(new Dimension(Surface.tile_x, Surface.tile_y));
		this.panel.add( c );
		//this.surface.pack();
	}
	
	public void addTile(Tile tile, int position) {
		this.tiles.get(position).setNewTile(tile);
				
		if(tile.isUpdateAble()) {
			this.surface.addTileToUpdater((UpdateableTile) tile);
		}
		this.panel.revalidate();
		this.panel.repaint();
	}
}
